from google.appengine.ext import db
from Common.CompanyOrg import Company
from Common.DuplicatedInstanceError import DuplicatedInstanceError

class Group(db.Model):
    GroupName = db.StringProperty(required=True)
    Ref_Company = db.ReferenceProperty(Company, collection_name='Groups')
    
    def LoadGroup(self, gName, p=None):
        return Group.get_by_key_name(gName, p)
    
    
    def put (self):
        # Make sure e-mails are unique for each user
        if (not self.is_saved()) and Group.get_by_key_name(self.GroupName, self._parent_key):
            raise DuplicatedInstanceError ('Group.GroupName', self.GroupName)
        # call the parent method
        db.Model.put (self)
        
        
    def TransactionPut(self):
        db.run_in_transaction(self.put)
        